package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.wxpay.sdk.Config;
import com.github.wxpay.sdk.WXPayRequest;
import com.github.wxpay.sdk.WXPayUtil;
import com.qingcheng.service.order.OrderService;
import com.qingcheng.service.pay.WeiXinPayService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信支付接口
 *
 * @author 岳剑飞
 * @Company http://www.ithiema.com
 */
@Service
public class WeiXInPayServiceImpl implements WeiXinPayService {


    @Autowired
    private Config config;

    /**
     * 生成微信支付二维码
     *
     * @param orderId   订单号
     * @param money     金额(分)
     * @param notifyUrl 回调地址
     * @return
     */
    @Override
    public Map createNative(String orderId, Integer money, String notifyUrl) {

        try {

            //1. 封装请求参数
            Map<String, String> map = new HashMap();  //创建参数集合
            map.put("appid", config.getAppID());  //公众账号ID
            map.put("mch_id", config.getMchID());    //商户号
            map.put("nonce_str", WXPayUtil.generateNonceStr());//随机字符串
            map.put("body", "青橙");  //商品描述
            map.put("out_trade_no", orderId);//订单号
            map.put("total_fee", money + "");//金额
            map.put("spbill_create_ip", "127.0.0.1"); // 终端IP
            map.put("notify_url", notifyUrl);  //回调通知地址
            map.put("trade_type", "NATIVE "); //交易类型

            String xmlParam = WXPayUtil.generateSignedXml(map, config.getKey());//MAP转换为XML字符串（自动添加签名)
            //xml 格式的参数
            System.out.println("参数: " + xmlParam);

            //2. 发送请求
            WXPayRequest wxPayRequest = new WXPayRequest(config);  //API的支付请求类
            String xmlResult = wxPayRequest.requestWithCert("/pay/unifiedorder", null, xmlParam, false);
            System.out.println("结果: " + xmlResult);

            //3. 解析返回结果
            Map<String, String> mapResult = WXPayUtil.xmlToMap(xmlResult);
            Map<String, String> map1 = new HashMap();
            map1.put("code_url", mapResult.get("code_url")); //支付地址
            map1.put("total_fee", money + "");   //总金额
            map1.put("out_trade_no", orderId);   //订单号
            return map1;
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap();
        }


    }


    @Autowired
    private OrderService orderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 微信支付回调
     *
     * @param xml
     */
    @Override
    public void notifyLogic(String xml) {


        try {
            //1. 对xml 进行解析 生成map
            Map<String, String> map = WXPayUtil.xmlToMap(xml);
            //2. 验证签名
            boolean signatureValid = WXPayUtil.isSignatureValid(map, config.getKey());
            System.out.println("验证签名是否正确: " + signatureValid);
            System.out.println(map.get("out_trade_no"));
            System.out.println(map.get("result_code"));
            rabbitTemplate.convertAndSend("paynotify","",map.get("out_trade_no"));
            orderService.updatePayStatus(map.get("out_trade_no"), map.get("transaction_id"));  //更新订单状态

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
